package benchmarks.tpcc;

import api.Configuration;
import api.result.Result;
import api.Worker;
import api.connection.DBConnection;

import java.util.Random;

public abstract class TPCCWorker extends Worker {
    protected final int terminalWarehouseID;
    protected final int terminalDistrictLowerID;
    protected final int terminalDistrictUpperID;
    protected final int numWarehouses;
    protected final Random gen = new Random(System.currentTimeMillis());

    public TPCCWorker(Configuration config, int terminalWarehouseID,
                      int terminalDistrictLowerID, int terminalDistrictUpperID,
                      int numWarehouses) {
        super(config);
        this.terminalWarehouseID = terminalWarehouseID;
        this.terminalDistrictLowerID = terminalDistrictLowerID;
        this.terminalDistrictUpperID = terminalDistrictUpperID;
        this.numWarehouses = numWarehouses;
    }

    public abstract TPCCTransaction makeTransactionImpl(String transactionName);

    /***
     * execute specific procedure
     * @param nextProcedure procedure name: "NewOrder", "Payment", "Delivery", "OrderStatus", "StockLevel"
     * @return
     */
    public Result executeProcedure(DBConnection connection, String nextProcedure) {
        TPCCTransaction transaction = makeTransactionImpl(nextProcedure);
        Result result = transaction.run(connection, gen, terminalWarehouseID, numWarehouses,
                terminalDistrictLowerID, terminalDistrictUpperID);
        return result;
    }
}
